using UnityEngine;

namespace System.Collections.Generic
{
    public static class CollectionsExtension
    {

        public static T ListLast<T>(this IReadOnlyList<T> list)
        {
            return list[list.Count - 1];
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="origin"></param>
        /// <param name="other"></param>
        public static void AddRange<K, V>(this IDictionary<K, V> origin, IDictionary<K, V> other)
        {
            foreach (var item in other)
            {
                origin.Add(item);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="origin"></param>
        /// <param name="other"></param>
        public static void AddRange<T>(this ICollection<T> origin, T[] other)
        {
            for (var i = 0; i < other.Length; i++)
            {
                origin.Add(other[i]);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="origin"></param>
        /// <param name="other"></param>
        public static void AddRange<T>(this ICollection<T> origin, IReadOnlyList<T> other)
        {
            for (var i = 0; i < other.Count; i++)
            {
                origin.Add(other[i]);
            }
        }

        public static void AddRange<T>(this ICollection<T> origin, HashSet<T> other)
        {
            foreach (var item in other)
            {
                origin.Add(item);
            }
        }

        /// <summary>
        /// 添加字典的Key
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="V"></typeparam>
        /// <param name="origin"></param>
        /// <param name="other"></param>
        public static void AddRangeKey<T, V>(this ICollection<T> origin, Dictionary<T, V> other)
        {
            foreach (var kv in other)
            {
                origin.Add(kv.Key);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="K"></typeparam>
        /// <param name="origin"></param>
        /// <param name="other"></param>
        public static void AddRangValue<T, K>(this ICollection<T> origin, Dictionary<K, T> other)
        {
            foreach (var kv in other)
            {
                origin.Add(kv.Value);
            }
        }


        /// <summary>
        /// 交换
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value1"></param>
        /// <param name="value2"></param>
        public static void Swap<T>(ref T value1, ref T value2)
        {
            var tmp = value1;
            value1 = value2;
            value2 = tmp;
        }

        public static Vector3 ToVector3(this IReadOnlyList<int> origin, float scale)
        {
            if (origin.Count != 3)
            {
                return Vector3.zero;
            }
            var v3 = new Vector3(origin[0], origin[1], origin[2]);
            return v3 / scale;
        }

        public static Vector2 ToVector2(this IReadOnlyList<int> origin, float scale)
        {
            if (origin.Count != 2)
            {
                return Vector2.zero;
            }
            var v3 = new Vector2(origin[0], origin[1]);
            return v3 / scale;
        }

        public static Vector3 ToVector3(this IReadOnlyList<float> origin)
        {
            if (origin.Count != 3)
            {
                return Vector3.zero;
            }
            var v3 = new Vector3(origin[0], origin[1], origin[2]);
            return v3;
        }

        public static Vector2 ToVector2(this IReadOnlyList<float> origin)
        {
            if (origin.Count != 2)
            {
                return Vector2.zero;
            }
            var v3 = new Vector2(origin[0], origin[1]);
            return v3;
        }
    }


}
